rm(list = setdiff(ls(), lsf.str()))

#Denmark vote intention 2010-2011-----------------
load("Study 1/Altered Data/Study1_Denmark.RData")
m10<-glm(vote_populist10_intention~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age+as.factor(v2010_education)+income + income_missing + zero1(econ_cons) + zero1(social), data=data, family="binomial")

DK_10<-data.frame(exp(cbind(coef(m10), confint(m10)))) 
DK_10<-DK_10[2,]                
colnames(DK_10 )[1]="Estimate"
colnames(DK_10 )[2]="Lo"
colnames(DK_10 )[3]="Up"
DK_10$sample<-"2010"
DK_10$Country<-"Denmark"

#Denmark vote choice in 2011
m11<-glm(vote_populist11_choice~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age+as.factor(v2010_education)+income + income_missing + zero1(econ_cons) + zero1(social), data=data, family="binomial")
DK_11<-data.frame(exp(cbind(coef(m11), confint(m11)))) 
DK_11<-DK_11[2,]                
colnames(DK_11)[1]="Estimate"
colnames(DK_11)[2]="Lo"
colnames(DK_11)[3]="Up"
DK_11$sample<-"2011"
DK_11$Country<-"Denmark"

#Netherlands Election 2017 -------------
load("Study 1/Altered Data/Study1_NL_17.RData")
m1<-glm(populist ~ zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age+ as.factor(education) + income + income_missing  + zero1(econ_cons) + zero1(anti_immi) , data=data, family="binomial")
NL_17<-data.frame(exp(cbind(coef(m1), confint(m1)))) 
NL_17<-NL_17[2,]                
colnames(NL_17 )[1]="Estimate"
colnames(NL_17 )[2]="Lo"
colnames(NL_17 )[3]="Up"
NL_17$sample<-"Election 2017"
NL_17$Country<-"Netherlands"

#Netherlands Election 2012 -------------
load("Study 1/Altered Data/Study1_NL_12.RData")
m2<-glm(populist ~ zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age + as.factor(education) +income + income_missing + zero1(econ_cons11) + zero1(anti_immi11), data=data, family="binomial")
NL_12<-data.frame(exp(cbind(coef(m2), confint(m2)))) 
NL_12<-NL_12[2,]                
colnames(NL_12 )[1]="Estimate"
colnames(NL_12 )[2]="Lo"
colnames(NL_12 )[3]="Up"
NL_12$sample<-"Election 2012"
NL_12$Country<-"Netherlands"

##Netherlands EU elections w1-------------
load("Study 1/Altered Data/Study1_NL_14.RData")
vote_intention<- glm(w1_pvv_national ~ zero1(w5_agre) + zero1(w5_open) + zero1(w5_con) + zero1(w5_extra) + zero1(w5_neu) + sex + age + prep_secon + highschool + secondary_vocation + pre_uni + college + uni + income + income_missing + zero1(w1_immi), data=data_sub, family="binomial")
EU_w1<-data.frame(exp(cbind(coef(vote_intention), confint(vote_intention)))) 
EU_w1<-EU_w1[2,]                
colnames(EU_w1 )[1]="Estimate"
colnames(EU_w1 )[2]="Lo"
colnames(EU_w1 )[3]="Up"
EU_w1$sample<-"EU 2014"
EU_w1$Country<-"Netherlands"

##Netherlands EU elections Vote Choice in EU elections-------------
vote_choiceNL<- glm(w4_pvv_EU_vote ~ zero1(w5_agre) + zero1(w5_open) + zero1(w5_con) + zero1(w5_extra) + zero1(w5_neu) + sex + age + prep_secon + highschool + secondary_vocation + pre_uni + college + uni + income + income_missing + zero1(w1_immi), data=data_sub, family="binomial")
EU_w4<-data.frame(exp(cbind(coef(vote_choiceNL), confint(vote_choiceNL)))) 
EU_w4<-EU_w4[2,]                
colnames(EU_w4 )[1]="Estimate"
colnames(EU_w4 )[2]="Lo"
colnames(EU_w4 )[3]="Up"
EU_w4$sample<-"EU 2015"
EU_w4$Country<-"Netherlands"

#Spain --------------------
load("Study 1/Altered Data/Study1_Spain.Rdata")

Podemos_full <-  miceadds::glm.cluster(populist_vote ~zero1(A_critical) + zero1(open) +zero1(con)+ zero1(ext) +zero1(neu) +female +age + income + income_missing + as.factor(education) + zero1(lr_placement), data=data, family=binomial, cluster="CCAA")

spain<-data.frame(exp(cbind(coef(Podemos_full), confint(Podemos_full)))) 
spain<-spain[2,]                
colnames(spain )[1]="Estimate"
colnames(spain )[2]="Lo"
colnames(spain )[3]="Up"
spain$sample<-"2016"
spain$Country<-"Spain"

#UK - Understanding Society Data ---------------------
load("Study 1/Altered Data/Study1_UK_understanding.RData")
vote.UKIP<- glm(total_populist ~zero1(w3_agre) + zero1(w3_open) + zero1(w3_con)+ zero1(w3_ext) + zero1(w3_neu) +female +age + Ed_OtherQualification + Ed_GSCElevel + Ed_Alevel + Ed_higherdegree + Ed_degree + Ed_Missing + income, data_UK, family=binomial)
UK_understanding<-data.frame(exp(cbind(coef(vote.UKIP), confint(vote.UKIP)))) 

UK_understanding<-UK_understanding[2,]                
colnames(UK_understanding )[1]="Estimate"
colnames(UK_understanding )[2]="Lo"
colnames(UK_understanding )[3]="Up"
UK_understanding$sample<-"Understanding Society"
UK_understanding$Country<-"United \n Kingdom"

#UK - British Election Studies -----------------------
load("Study 1/Altered Data/Study1_UK_BES.RData")
vote.UKIPw6 <- glm(w6_voteUKIP ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w4_immiatt) + zero1(w6_redistribution), data=data_BES, family="binomial")
UK_bes<-data.frame(exp(cbind(coef(vote.UKIPw6), confint(vote.UKIPw6)))) 
UK_bes_agre<-UK_bes[2,]                
colnames(UK_bes_agre )[1]="Estimate"
colnames(UK_bes_agre )[2]="Lo"
colnames(UK_bes_agre )[3]="Up"
UK_bes_agre$sample<-"UK Election 2015"
UK_bes_agre$Country<-"United \n Kingdom"

#Meta analysis---------------------
load("Study 1/Altered Data/Meta_study1.RData")
#Faster but less accurate estimate
model2 <- glmer(populist ~ agre + open + con + ext + neu +age + female + education + income + income_missing + social_cons +social_cons_missing  + econ_cons + econ_cons_missing + lr_placement + lr_placement_missing + authoritarianism + authoritarianism_missing + as.factor(language) + as.factor(study) + (1|study) + (1|id) , data=data_meta, family="binomial", nAGQ=0) #faster but less accurate
#calculate Odds ratios
meta<-data.frame(exp(cbind(OR=fixef(model2),confint(model2,parm="beta_",method="Wald"))))
meta<-meta[2,]                
colnames(meta )[1]="Estimate"
colnames(meta )[2]="Lo"
colnames(meta )[3]="Up"
meta$sample<-"Pooled"
meta$Country<-"Pooled\n analysis"


#calculate 1 SD above and below the mean
low  <- mean(data_meta$agre, na.rm=T)-sd(data_meta$agre, na.rm=T)
high <- mean(data_meta$agre, na.rm=T)+sd(data_meta$agre, na.rm=T)

# Why not write that the odds are 1.5 times higher?
odds.difference <- (fixef(model2)[2]*low) / (fixef(model2)[2]*high)
1/odds.difference #1.67 more likely to vote for a populist


#Create plot -------------
comb_vote<-rbind(meta, UK_understanding, UK_bes_agre, NL_12, NL_17, EU_w1, EU_w4, spain, DK_10, DK_11)
comb_vote$sample <-factor(comb_vote$sample, levels=c('Pooled', 'Understanding Society', 'UK Election 2015', 'Election 2017', 'EU 2015', 'EU 2014', 'Election 2012','2016', '2011', '2010'))
comb_vote$Country <-factor(comb_vote$Country, levels=c('United \n Kingdom', 'Denmark', 'Netherlands', 'Spain', 'Pooled\n analysis'))

#Plot figure------------
fig2 <- ggplot(comb_vote ,aes(x=sample, y=Estimate, group=Country))+geom_pointrange(aes(ymin=Lo,ymax=Up), size=1)  + theme_bw()+theme(legend.position="off")+ylab("Odds ratio of voting for a populist party/politician")+xlab("")+geom_hline(yintercept=1,linetype = "dashed", color="red") + coord_flip() + facet_grid(Country~., scales = "free_y")+ theme(axis.text.x=element_text(size=14), strip.text.y=element_text(size=14)) + geom_rect(data = subset(comb_vote,Country == 'Meta \n analysis'),aes(fill = Country),xmin = -Inf,xmax = Inf, ymin = -Inf,ymax = Inf, alpha = 0.3) +  theme(strip.text.y = element_text(angle = 360))

ggsave(fig2, file="Figures/fig1_meta_nocynicism.pdf", dpi=900, width = 7, height =8)

